//
//  QGrabBottomCell.m
//  QoilPublic
//
//  Created by 杨凯 on 16/9/6.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QGrabBottomCell.h"

@interface QGrabBottomCell()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *idTextField;

@property (weak, nonatomic) IBOutlet UIButton *photoBtn;

@property (weak, nonatomic) IBOutlet UIButton *idCardzBtn;

@property (weak, nonatomic) IBOutlet UIButton *idCardfBtn;

@end

@implementation QGrabBottomCell

- (void)awakeFromNib {
    [super awakeFromNib];
    _idTextField.delegate = self;
}

- (void)setImage_1:(UIImage *)image_1{

    _image_1 = image_1;
    [_photoBtn setImage:_image_1 forState:UIControlStateNormal];
}

- (void)setImage_2:(UIImage *)image_2{

    _image_2 = image_2;
    [_idCardzBtn setImage:_image_2 forState:UIControlStateNormal];

}

- (void)setImage_3:(UIImage *)image_3{

    _image_3 = image_3;
    [_idCardfBtn setImage:_image_3 forState:UIControlStateNormal];

}


- (IBAction)photoAction:(id)sender {
    
    if(_oneBlock){
        _oneBlock();
    }
}

- (IBAction)idCardzAction:(id)sender {
    if(_twoBlock){
        _twoBlock();
    }
}

- (IBAction)idCardfAction:(id)sender {
    if(_threeBlock){
        _threeBlock();
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}


-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *text = [textField text];
    
    NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\b"];
    string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
    if ([string rangeOfCharacterFromSet:[characterSet invertedSet]].location != NSNotFound) {
        return NO;
    }
    
    text = [text stringByReplacingCharactersInRange:range withString:string];
    text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    NSString *newString = @"";
    while (text.length > 0) {
        NSString *subString = [text substringToIndex:MIN(text.length, 6)];
        newString = [newString stringByAppendingString:subString];
        if (subString.length == 6) {
            newString = [newString stringByAppendingString:@" "];
        }
        text = [text substringFromIndex:MIN(text.length, 6)];
    }
    
    newString = [newString stringByTrimmingCharactersInSet:[characterSet invertedSet]];
    
    if (newString.length >= 21) {
        return NO;
    }
    
    [textField setText:newString];
    if(_idCardBlock){
        _idCardBlock(textField.text);
    }

    return NO;
}

@end
